﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace I_C
{
    public partial class MainForm : Form
    {
        #region =================================== Internal Data
        // ========== Channel Viewer
        ChannelDisplay channelDisplay;
        I2CDevice.OPERATION lastOperation;

        #endregion
        public MainForm()
        {
            InitializeComponent();

            channelDisplay = new ChannelDisplay();
        }

        private void canvasPanel_Paint(object sender, PaintEventArgs e)
        {
            channelDisplay.Paint(e.Graphics, canvasPanel.Width, canvasPanel.Height);
        }

        private void canvasPanel_Resize(object sender, EventArgs e)
        {
            canvasPanel.Invalidate();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            channelDisplay.Update(I2CDevice.OPERATION.START, 0, "Start");

            btnStart.Enabled = false;
            btnStop.Enabled = false;
            btnRestart.Enabled = false;
            btnAddress.Enabled = true;
            btnSend.Enabled = false;
            btnAck.Enabled = false;
            btnNack.Enabled = false;
            btnMasterAck.Enabled = false;
            btnMasterNack.Enabled = false;
            btnSlaveSend.Enabled = false;

            canvasPanel.Invalidate();
            lastOperation = I2CDevice.OPERATION.START;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            channelDisplay.Update(I2CDevice.OPERATION.STOP, 0, "Stop");

            btnStart.Enabled = true;
            btnStop.Enabled = false;
            btnRestart.Enabled = false;
            btnAddress.Enabled = false;
            btnSend.Enabled = false;
            btnAck.Enabled = false;
            btnNack.Enabled = false;
            btnMasterAck.Enabled = false;
            btnMasterNack.Enabled = false;
            btnSlaveSend.Enabled = false;
            
            canvasPanel.Invalidate();
            lastOperation = I2CDevice.OPERATION.STOP;
        }

        private void btnRestart_Click(object sender, EventArgs e)
        {
            channelDisplay.Update(I2CDevice.OPERATION.RESTART, 0, "Restart");

            btnStart.Enabled = false;
            btnStop.Enabled = false;
            btnRestart.Enabled = false;
            btnAddress.Enabled = true;
            btnSend.Enabled = false;
            btnAck.Enabled = false;
            btnNack.Enabled = false;
            btnMasterAck.Enabled = false;
            btnMasterNack.Enabled = false;
            btnSlaveSend.Enabled = false;

            canvasPanel.Invalidate();
            lastOperation = I2CDevice.OPERATION.RESTART;
        }

        private void btnAck_Click(object sender, EventArgs e)
        {
            channelDisplay.Update(I2CDevice.OPERATION.ACK, 0, "ACK");

            btnStart.Enabled = false;
            btnStop.Enabled = true;
            btnRestart.Enabled = true;
            btnAddress.Enabled = false;
            btnSend.Enabled = true;
            btnAck.Enabled = false;
            btnNack.Enabled = false;
            btnMasterAck.Enabled = false;
            btnMasterNack.Enabled = false;
            btnSlaveSend.Enabled = false;

            canvasPanel.Invalidate();

            if (lastOperation == I2CDevice.OPERATION.SEND)
            {
                txtSlaveIn.Text = txtMasterOut.Text;
                txtSlaveIn.Invalidate();
            }
            else if (lastOperation == I2CDevice.OPERATION.SEND_ADDRESS)
            {
                if (Byte.Parse(txtRW.Text, NumberStyles.HexNumber) == 1)    // the master wants data
                {
                    btnSlaveSend.Enabled = true;
                    btnSend.Enabled = false;
                    btnStop.Enabled = false;
                    btnRestart.Enabled = false;
                }
            }
            lastOperation = I2CDevice.OPERATION.ACK;
        }

        private void btnNack_Click(object sender, EventArgs e)
        {
            channelDisplay.Update(I2CDevice.OPERATION.NACK, 0, "NACK");
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            btnRestart.Enabled = true;
            btnAddress.Enabled = false;
            btnSend.Enabled = false;
            btnAck.Enabled = false;
            btnNack.Enabled = false;
            btnMasterAck.Enabled = false;
            btnMasterNack.Enabled = false;
            btnSlaveSend.Enabled = false;
            canvasPanel.Invalidate();
            lastOperation = I2CDevice.OPERATION.NACK;
        }

        private void btnAddress_Click(object sender, EventArgs e)
        {
            try
            {
                byte address = Byte.Parse(txtMasterAddress.Text, NumberStyles.HexNumber);
                address <<= 1;
                address |= Byte.Parse(txtRW.Text, NumberStyles.HexNumber);
                if ((address & 0x1) != 0)
                {
                    channelDisplay.Update(I2CDevice.OPERATION.SEND_ADDRESS, address, "Bits 7-1: Address" + (txtMasterAddress.Text) + " .   Bit 0: R/W bit is 1: The master wants data");
                }
                else
                {
                    channelDisplay.Update(I2CDevice.OPERATION.SEND_ADDRESS, address, "Bits 7-1: Address" + (txtMasterAddress.Text) + " .   Bit 0: R/W bit is 0: The master sends data");
                }
                btnStart.Enabled = false;
                btnStop.Enabled = false;
                btnRestart.Enabled = false;
                btnAddress.Enabled = false;
                btnSend.Enabled = false;
                btnAck.Enabled = true;
                btnNack.Enabled = true;
                btnMasterAck.Enabled = false;
                btnMasterNack.Enabled = false;
                btnSlaveSend.Enabled = false;
                canvasPanel.Invalidate();
                lastOperation = I2CDevice.OPERATION.SEND_ADDRESS;
            }
            catch (Exception)
            {
            }
            
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                channelDisplay.Update(I2CDevice.OPERATION.SEND, Byte.Parse(txtMasterOut.Text, NumberStyles.HexNumber), "Data from Saster: " + txtMasterOut.Text);
                btnStart.Enabled = false;
                btnStop.Enabled = true;
                btnRestart.Enabled = false;
                btnAddress.Enabled = false;
                btnSend.Enabled = false;
                btnAck.Enabled = true;
                btnNack.Enabled = true;
                btnMasterAck.Enabled = false;
                btnMasterNack.Enabled = false;
                btnSlaveSend.Enabled = false;
                canvasPanel.Invalidate();
                lastOperation = I2CDevice.OPERATION.SEND;
            }
            catch (Exception)
            {
            } 
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            channelDisplay.Clear();
            btnStart.Enabled = true;
            btnStop.Enabled = false;
            btnRestart.Enabled = false;
            btnAddress.Enabled = false;
            btnSend.Enabled = false;
            btnAck.Enabled = false;
            btnNack.Enabled = false;
            btnMasterAck.Enabled = false;
            btnMasterNack.Enabled = false;
            btnSlaveSend.Enabled = false;
            canvasPanel.Invalidate();
            lastOperation = I2CDevice.OPERATION.ANY;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;
            Left = 0;
        }

        private void btnSlaveSend_Click(object sender, EventArgs e)
        {
            

            try
            {
                channelDisplay.Update(I2CDevice.OPERATION.SEND, Byte.Parse(txtSlaveOut.Text, NumberStyles.HexNumber), "Data From Slave: " + txtSlaveOut.Text);
                btnStart.Enabled = false;
                btnStop.Enabled = true;
                btnRestart.Enabled = false;
                btnAddress.Enabled = false;
                btnSend.Enabled = false;
                btnAck.Enabled = false;
                btnNack.Enabled = false;
                btnMasterAck.Enabled = true;
                btnMasterNack.Enabled = true;
                btnSlaveSend.Enabled = false;
                canvasPanel.Invalidate();
                lastOperation = I2CDevice.OPERATION.ANY;
            }
            catch (Exception)
            {
            }
        }

        private void btnMasterAck_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnStop.Enabled = false;
            btnRestart.Enabled = false;
            btnAddress.Enabled = false;
            btnSend.Enabled = false;
            btnAck.Enabled = false;
            btnNack.Enabled = false;
            btnMasterAck.Enabled = false;
            btnMasterNack.Enabled = false;
            btnSlaveSend.Enabled = true;
            txtMasterIn.Text = txtSlaveOut.Text;
            txtMasterIn.Invalidate();
            canvasPanel.Invalidate();
            lastOperation = I2CDevice.OPERATION.ANY;
        }

        private void btnMasterNack_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            btnRestart.Enabled = true;
            btnAddress.Enabled = false;
            btnSend.Enabled = false;
            btnAck.Enabled = false;
            btnNack.Enabled = false;
            btnMasterAck.Enabled = false;
            btnMasterNack.Enabled = false;
            btnSlaveSend.Enabled = false;
            txtMasterIn.Text = txtSlaveOut.Text;
            txtMasterIn.Invalidate();
            canvasPanel.Invalidate();
            lastOperation = I2CDevice.OPERATION.ANY;
        }
    }
}
